package com.yanqu.road.dao.impl.curio;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.curio.UserCurioData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserCurioDataDaoImpl extends TempDataObjectDao<UserCurioData> {

    public static String getUserCurioDataSql = "select * from `t_u_curio` where `userId`=?";
    public UserCurioData getUserCurioData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserCurioDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserCurioDataSql = "update `t_u_curio` set score=?,badgeLv=?,drawTimes=?,qualityDrawTimesMap=?,scoreUpdateTime=?,riverProcess=? where userId=?";
    @Override
    protected boolean updateData(UserCurioData userCurioData) {
        userCurioData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getBadgeLv()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userCurioData.getQualityDrawTimesMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getScoreUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getRiverProcess()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getUserId()));
        if (getDb().executeNoneQuery(updateUserCurioDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserCurioDataSql = "insert into `t_u_curio`(userId,score,badgeLv,drawTimes,qualityDrawTimesMap,scoreUpdateTime,riverProcess) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserCurioData userCurioData) {
        userCurioData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getBadgeLv()));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userCurioData.getQualityDrawTimesMap())));
        para.put(i++, new DbParameter(Types.INTEGER, userCurioData.getScoreUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userCurioData.getRiverProcess()));
        if (getDb().executeNoneQuery(insertUserCurioDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserCurioData parseData(ResultSet rs) throws SQLException {
        UserCurioData userCurioData = new UserCurioData();
        userCurioData.setUserId(rs.getLong("userId"));
        userCurioData.setScore(rs.getLong("score"));
        userCurioData.setBadgeLv(rs.getInt("badgeLv"));
        userCurioData.setDrawTimes(rs.getInt("drawTimes"));
        userCurioData.setQualityDrawTimesMap(JSON.parseObject(rs.getString("qualityDrawTimesMap"), new TypeReference<Map<Integer, Integer>>() {}));
        userCurioData.setScoreUpdateTime(rs.getInt("scoreUpdateTime"));
        userCurioData.setRiverProcess(rs.getLong("riverProcess"));
        return userCurioData;
    }
}
